package jp.heartdokyun.model;

import jp.heartdokyun.activity.GameActivity;
import jp.heartdokyun.model.component.Behavior;
import jp.heartdokyun.model.component.BehaviorA;
import jp.heartdokyun.model.component.BehaviorB;
import jp.heartdokyun.model.component.BehaviorC;
import jp.heartdokyun.model.component.BehaviorD;
import jp.heartdokyun.model.component.BehaviorE;
import jp.heartdokyun.model.component.BehaviorF;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.view.MotionEvent;

/**
 * @author bon
 * 
 */
public class Heart {

	// private int maxTimes;
	private Bitmap mBitmap, mBitmapTouch; // the actual bitmap
	private int x; // the X coordinate
	private int y; // the Y coordinate
	private boolean touched; // if droid is touched/picked up
	private int mHeartType;
	private Behavior behavior;
	private int mHeartTouchDelay;

	public Heart(Bitmap bitmap, Bitmap bitmapTouch, int x, int y, int type,
			int speed, int angleSpeed, int distance) {
		this.mBitmap = bitmap;
		this.mBitmapTouch = bitmapTouch;
		mHeartType = type;
		switch (type) {
		case 0:
			this.setBehavior(new BehaviorA(speed, angleSpeed, distance, x, y));
			break;
		case 1:
			this.setBehavior(new BehaviorB(speed, angleSpeed, distance, x, y));
			break;
		case 2:
			this.setBehavior(new BehaviorC(speed, angleSpeed, distance, x, y));
			break;
		case 3:
			this.setBehavior(new BehaviorD(speed, angleSpeed, distance, x, y));
			break;
		case 4:
			this.setBehavior(new BehaviorE(speed, angleSpeed, distance, x, y));
			break;
		case 5:
			this.setBehavior(new BehaviorF(speed, angleSpeed, distance, x, y));
			break;
		default:
			throw new IllegalArgumentException("unknown type");
		}

		// this.x =
		// this.y =
		this.x = x + this.behavior.getInitX();
		this.y = y + this.behavior.getInitY();
	}

	private void setBehavior(Behavior behavior) {
		this.behavior = behavior;
	}

	// public int getMaxTimes() {
	// return maxTimes;
	// }
	//
	// public void setMaxTimes(int maxTimes) {
	// this.maxTimes = maxTimes;
	// }

	// public Bitmap getBitmap() {
	// return bitmap;
	// }
	//
	// public void setBitmap(Bitmap bitmap) {
	// this.bitmap = bitmap;
	// }

	public int getX() {
		return x;
	}

	public void setX(int x) {
		this.x = x;
	}

	public int getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	// public boolean isTouched() {
	// return touched;
	// }
	//
	// public void setTouched(boolean touched) {
	// this.touched = touched;
	// }

	public void recycleBitmap() {
		if (mBitmap != null)
			mBitmap.recycle();
		if (mBitmapTouch != null)
			mBitmapTouch.recycle();
	}

	public void draw(Canvas canvas) {
		if (!touched) {
			canvas.drawBitmap(mBitmap, x - (mBitmap.getWidth() / 2), y
					- (mBitmap.getHeight() / 2), null);
		} else {
			canvas.drawBitmap(mBitmapTouch, x - (mBitmapTouch.getWidth() / 2),
					y - (mBitmap.getHeight() / 2), null);
			if (mHeartTouchDelay == 4)
				touched = !touched;
			mHeartTouchDelay++;
		}
	}

	/**
	 * Method which updates the droid's internal state every tick
	 */
	public void update(Canvas canvas) {
		behavior.caliculateNextPosition(x, y, canvas.getWidth(),
				canvas.getHeight(), mBitmap.getWidth(), mBitmap.getHeight());
		x = behavior.getNextX();
		y = behavior.getNextY();
	}

	public boolean isAvail() {
		return this.behavior.isAvail();
	}

	/**
	 * Handles the {@link MotionEvent.ACTION_DOWN} event. If the event happens
	 * on the bitmap surface then the touched state is set to <code>true</code>
	 * otherwise to <code>false</code>
	 * 
	 * @param eventX
	 *            - the event's X coordinate
	 * @param eventY
	 *            - the event's Y coordinate
	 */
	public void handleActionDown(int eventX, int eventY) {
		if (eventX >= (x - mBitmap.getWidth() / 2)
				&& (eventX <= (x + mBitmap.getWidth() / 2))) {
			if (eventY >= (y - mBitmap.getHeight() / 2)
					&& (eventY <= (y + mBitmap.getHeight() / 2))) {
				touched = true;
				mHeartTouchDelay = 0;
				switch (mHeartType) {
				case 0:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(500);
					break;
				case 1:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(500);
					break;
				case 2:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(700);
					break;
				case 3:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(700);
					break;
				case 4:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(1000);
					break;
				case 5:
					// GameActivity.updateHeartPercent(Resources.getSystem()
					// .getInteger(R.integer.game_hearta_point));
					GameActivity.updateHeartPercent(2000);
					break;
				default:
					GameActivity.updateHeartPercent(0);
					break;
				}
			}
		}
	}
}
